/**
 * 
 */
package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.FundInfoProcessor;
import tech.abing.spider.task.pageprocessor.amac.PrivateDirectInvestMentProcessor;
import tech.abing.spider.task.pipeline.amac.FundInfoPipeline;
import tech.abing.spider.task.pipeline.amac.PrivateDirectInvestmentFundPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 类说明 证券公司资管产品
 * @author HUNING
 * @since 2018-1-4
 * 
 */
public class PrivateDirectInvestmentFundSpider {

	private static SqlUtil sqlUtil = null;

	public static void start() {
		CrawlManager fundInfo = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = fundInfo
		.prepareCrawlTask(DictEnum.TASK_TYPE_PRIVATE_DIRECT_INVESTMENT_FUND.getName(), DefaultPageDownloader.class)
		.useThread(20)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(200)
		.usePipeline(PrivateDirectInvestmentFundPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){
			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_PRIVATE_DIRECT_INVESTMENT_FUND.getCode());
				sqlUtil.deleteForConn("delete from PRIVATE_DIRECT_INVESTMENT_FUND_STAT");
			}
			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		CrawlTask task = builder.build();
		fundInfo.start(task);
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
//			String url = "http://gs.amac.org.cn/amac-infodisc/api/pof/securities/pages/amacWeb/user!list.action?" +
//					"filter_LIKES_CPMC=&filter_LIKES_GLJG=&filter_LIKES_CPBM=&filter_GES_SLRQ=&filter_LES_SLRQ=" +
//					"&page.searchFileName=publicity_web&page.sqlKey=PAGE_PUBLICITY_WEB&page.sqlCKey=SIZE_PUBLICITY_WEB" +
//					"&_search=false&nd=0.8094076194970157&page.pageSize=20&page.pageNo=1&page.orderBy=SLRQ&page.order=desc";
			String url = "http://gs.amac.org.cn/amac-infodisc/api/pof/securities?rand=0.7476623745543449&page=0&size=20" ;
	
			PageRequest req = context.createPageRequest(url, PrivateDirectInvestMentProcessor.class);
			
			req.setMethod(Method.POST);
			req.setPostType(PostType.REQUEST_PAYLOAD);
			req.setReqPayloadParams("{}");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
			req.putHeader("Referer", "http://gs.amac.org.cn/amac-infodisc/res/pof/securities/index.html");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			req.putHeader("Host", "gs.amac.org.cn");
			req.putHeader("Content-Type", "application/json");
			req.putHeader("Origin", "http://gs.amac.org.cn");
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Encoding", "gzip, deflate");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.9");
			req.putHeader("Connection", "keep-alive");
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}
}
